package com.sociesc.findasmartphonespark.wekaUtils;

import com.sociesc.findasmartphonespark.model.AccessPoint;
import weka.classifiers.bayes.BayesNet;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ArffLoader;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by DanielArthur on 31/12/2014.
 */
public class Classificador {
    /**
     * @param
     */
    /*public static String iniciar(File entrada, String saida){
        // caminho dos arquivos de entrada para treino
        //File path = new File("C:\\Users\\DanielArthur\\Desktop\\WifiSignal\\");
        if (entrada.exists()) {
            String retornoGeraArff = geraArff.iniciar(entrada, saida);     // gera arquivo arffFinal
            if (retornoGeraArff == "1"){
                try {
                    BayesNet BayesNet = IncrementalClassifier.treinar(saida);
                    classificar(BayesNet);
                    return "ok";
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    return "erro no classificador";
                }
            } else {
                return "erro ao gerar arff";
            }
        } else {
            System.out.println("Pasta com as informacoes para treinar o classificador nao existe:\n" + entrada);
            return "erro caminho nao existe";
        }
    }*/

    public static BayesNet treinar(String arffSaida) {
        BayesNet BayesNet = new BayesNet();
        try {
            BayesNet = IncrementalClassifier.treinar(arffSaida);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return BayesNet;
    }

    public static String classificarArff(BayesNet BayesNet, String entrada) throws IOException {
        // testa com os mesmos valores do treinamento
        // Usando um ARFF
        Instance current;

        String retorno = "";

        ArffLoader loader = new ArffLoader();
        loader.setFile(new File(entrada));
        Instances structure = loader.getStructure();
        structure.setClassIndex(structure.numAttributes() - 1);

        double pred = 0;

        while ((current = loader.getNextInstance(structure)) != null) {
            try {
                pred = BayesNet.classifyInstance(current);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return e.toString();
            }
            /*retirado de dentro do while*/
            System.out.println("Class predicted: "
                    + current.classAttribute().value((int) pred));
            //return current.classAttribute().value((int) pred);
            retorno = current.classAttribute().value((int) pred);
        }

        return "Class predicted: " + retorno;
    }
}
